package com.platform.framework.serviceimpl.alipay;



import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.platform.framework.dao.alipay.AlipayReturnInfoMybatisDao;
import com.platform.framework.dao.alipay.AliRefundReturnInfoMybatisDao;
import com.platform.framework.dao.pub.TradeFlowMybatisDao;
import com.platform.framework.entity.alipay.AlipayReturnInfo;
import com.platform.framework.entity.alipay.AliRefundReturnInfo;
import com.platform.framework.entity.pub.TradeFlow;
import com.platform.framework.gateway.alipay.inf.AlipayGateway;
import com.platform.framework.serviceinf.alipay.AlipayService;
import com.platform.framework.util.pub.ReflectUtil;
import com.platform.framework.util.pub.Validation;
import com.platform.framework.vo.pub.ResponseVo;
import com.platform.framework.vo.states.PayResult;
import com.platform.framework.vo.transfer.TransportLayer;

/**
 * AlipayService实现类
 * @author ly-liugaoyang
 *
 */
@Service("alipayService")
public class AlipayServiceImpl implements AlipayService {
	
	private Logger logger = Logger.getLogger(AlipayServiceImpl.class);
	
	@Resource
	private TradeFlowMybatisDao tradeFlowMybatisDao;
	
	@Resource
	private AlipayReturnInfoMybatisDao alipayReturnInfoMybatisDao;
	
	@Resource
	private AliRefundReturnInfoMybatisDao aliRefundReturnInfoMybatisDao;
	
	@Resource
	private AlipayGateway alipayGateway;

	@Override
	@Transactional(readOnly = false, propagation=Propagation.REQUIRED)
	public ResponseVo aliBarCodePay(TransportLayer transportLayer)
			throws Exception {
		try {
			TradeFlow tradeFlow = tradeFlowMybatisDao.getById(Integer.parseInt(transportLayer.getTradeFlowId()));
			AlipayReturnInfo alipayReturnInfo = alipayGateway.aliBarcodePay(transportLayer);
			tradeFlow.setPayResult(alipayReturnInfo.getPayResult());
			tradeFlow.setBuyerUserId(alipayReturnInfo.getBuyerUserId());
			alipayReturnInfo.setTradeFlowId(tradeFlow.getId());
			tradeFlowMybatisDao.update(tradeFlow);
			alipayReturnInfoMybatisDao.save(alipayReturnInfo);
			return new ResponseVo(tradeFlow.getPayResult(), alipayReturnInfo.getSubCode(), alipayReturnInfo.getSubMsg(), tradeFlow.getId()); // 返回交易流水号
		} catch (Exception e) {
			logger.error("[tradelog:"+transportLayer.getOrderNo()+"]AlipayServiceImpl-->aliBarcodePay() Method Error,exception is :"+ e);
			e.printStackTrace();
			return new ResponseVo(PayResult.ERROR, null, null, Integer.parseInt(transportLayer.getTradeFlowId()));
		}
	}

	@Override
	@Transactional(readOnly = false, propagation=Propagation.REQUIRED)
	public ResponseVo aliQRCodePay(TransportLayer transportLayer)
			throws Exception {
		String qrCode = alipayGateway.generateQRCode(transportLayer);
		return new ResponseVo(PayResult.TRADE_SUCCESS, null, null, Integer.parseInt(transportLayer.getTradeFlowId()),qrCode);
	}

	@Override
	public ResponseVo aliPCPay(TransportLayer transportLayer) {
		String html = alipayGateway.aliPCpay(transportLayer);
		return new ResponseVo(PayResult.ON_TRADING, html, "pc pay return success.", Integer.parseInt(transportLayer.getTradeFlowId()));
	}

	@Override
	public ResponseVo aliWAPPay(TransportLayer transportLayer) {
		String html = alipayGateway.aliWapPay(transportLayer);
		return new ResponseVo(PayResult.ON_TRADING, html, "wap pay return success.", Integer.parseInt(transportLayer.getTradeFlowId()));
	}

	@Override
	public ResponseVo aliRefund(TransportLayer transportLayer) {
		try {
			TradeFlow tradeFlow = tradeFlowMybatisDao.getById(Integer.parseInt(transportLayer.getTradeFlowId()));
			AliRefundReturnInfo alirefundReturnInfo = alipayGateway.aliTradeRefund(transportLayer);
			tradeFlow.setPayResult(alirefundReturnInfo.getPayResult());
			alirefundReturnInfo.setTradeFlowId(tradeFlow.getId());
			tradeFlowMybatisDao.update(tradeFlow);
			aliRefundReturnInfoMybatisDao.save(alirefundReturnInfo);
			return new ResponseVo(tradeFlow.getPayResult(), alirefundReturnInfo.getSubCode(), alirefundReturnInfo.getSubMsg(), tradeFlow.getId()); // 返回交易流水号
		} catch (Exception e) {
			logger.error("[tradelog:"+transportLayer.getOrderNo()+"]AlipayServiceImpl-->aliQRCodePay() Method Error,exception is :"+ e);
			e.printStackTrace();
			return new ResponseVo(PayResult.ERROR, null, null, Integer.parseInt(transportLayer.getTradeFlowId()));
		}
	}

	@Override
	public ResponseVo aliSettle(TransportLayer transportLayer) {
		String codeUrl = "";
		try {
			codeUrl = alipayGateway.generateQRCode(transportLayer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseVo(PayResult.ON_TRADING, codeUrl, null, Integer.parseInt(transportLayer.getTradeFlowId()));
	}

	@Override
	public ResponseVo aliQueryTradeResult(TransportLayer transportLayer)
			throws Exception {
		TradeFlow tradeFlow = ReflectUtil.voPersistence(new TradeFlow(), transportLayer);
		if (!Validation.isEmpty(transportLayer.getTradeFlowId())) {
			tradeFlow.setId(Integer.parseInt(transportLayer.getTradeFlowId()));
		}
		List<TradeFlow> list = tradeFlowMybatisDao.findTradeFlow(tradeFlow);
		if(Validation.isEmptyList(list)){
			return null;
		}
		List<TradeFlow> resultList = new ArrayList<TradeFlow>();
		for (TradeFlow tradeFlow2 : list) {
			if (!Validation.isEmpty(tradeFlow2.getPayResult())) {
				resultList.add(tradeFlow2);
			}
		}
		return new ResponseVo(PayResult.TRADE_SUCCESS, null, "querysuccess", null, resultList);
	}

	@Override
	public ResponseVo aliCancelTrade(TransportLayer transportLayer)
			throws Exception {
		return null;
	}
	
	
}
